#!/usr/bin/env python
#coding:utf-8
#Code by Mango
#Created Time: Sat 19 Jul 2014 05:23:28 PM CST

#处理时间戳的

import pickle
from datetime import datetime
import socket
import time
import sys

if len(sys.argv) < 2:
    sys.exit(1)

a = sys.argv[1]
b = sys.argv[2]

def get_dump(a, b):
    host,port = a, b
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, port))
    s.send("up")
    s.close()

#def load_data_time():
get_dump(a, int(b))
f = file('h_dic.pkl', 'rb')
host_status = pickle.load(f)  #把pickle后的数据还原为字典

for h,m in host_status.items():
#print h,m[-1][0]
    if len(m) != 0:
        oldtime = m[-1][0]
        time_diff = (datetime.now() - oldtime).seconds
        if time_diff > 30:
            print "No data received from %s for %s,check!" % (h,time_diff)
        else:
            print h,(datetime.now() - oldtime).seconds


#get_dump(a, int(b))
#load_data_time()
#time.sleep(20)
    
